//
//  LYLCarouseFigure.m
//  
//
//  Created by dllo on 15/11/26.
//
//

#import "LYLCarouseFigure.h"

#define WDTH self.frame.size.width
#define HEGHT self.frame.size.height


@interface LYLCarouseFigure ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UIScrollView *scrollerView;
@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, assign) NSInteger count;
@property (nonatomic, assign) NSInteger countOne;
@property (nonatomic, assign) NSInteger countTwo;
@property (nonatomic, assign) NSInteger time;


@end
@implementation LYLCarouseFigure



- (instancetype)initWithImageURLArr:(NSMutableArray *)imageArr IntervalTime:(CGFloat)time Frame:(CGRect)frame PageControl:(BOOL)pageControl
{
    self = [super initWithFrame:frame];
    if (self) {
        self.count = imageArr.count;
        self.countOne = imageArr.count + 1;
        self.countTwo = imageArr.count + 2;
        self.time = time;
        // 创建 scrollerView 加到 self
        self.scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,WDTH, HEGHT)];
        _scrollerView.contentSize = CGSizeMake(_countTwo *WDTH, 0);
        _scrollerView.showsHorizontalScrollIndicator = NO;
        _scrollerView.showsVerticalScrollIndicator = NO;
        _scrollerView.bounces = NO;
        _scrollerView.pagingEnabled = YES;
        _scrollerView.delegate = self;
        [self addSubview:_scrollerView];
        
        for (NSInteger i = -1; i; i++) {
            UIImageView *tempImageView = [[UIImageView alloc] init];
            [_scrollerView addSubview:tempImageView];
            if (i == -1) {
                tempImageView.frame = CGRectMake(0, 0, WDTH, HEGHT);
                NSURL *url = [NSURL URLWithString:imageArr.lastObject];
                [tempImageView sd_setImageWithURL:url];
            } else if (i < _countOne - 1)
            {
                tempImageView.frame = CGRectMake(WDTH * (i + 1), 0, WDTH, HEGHT);
                NSURL *url = [NSURL URLWithString:imageArr[i]];
                [tempImageView sd_setImageWithURL:url];
            } else
            {
                tempImageView.frame = CGRectMake(WDTH * (i + 1), 0, WDTH, HEGHT);
                NSURL *url = [NSURL URLWithString:imageArr.firstObject];
                [tempImageView sd_setImageWithURL:url];
            }
        }
        // 首次 ScroolView 偏移量
        _scrollerView.contentOffset = CGPointMake(WDTH, 0);
    }
    // 创建 PageControl 加到 self
    if (pageControl == YES) {
        self.pageControl = [[UIPageControl alloc] init];
        [self addSubview:_pageControl];
        _pageControl.numberOfPages = _count;
        _pageControl.currentPage = 0;
        CGSize size = [_pageControl sizeForNumberOfPages:_count];
        // 可修改 pageControl 坐标
        _pageControl.bounds = CGRectMake(0, 0, size.width, size.height);
        _pageControl.center = CGPointMake(WDTH / 2, HEGHT - 20);
        
        //设置颜色
        _pageControl.pageIndicatorTintColor = [UIColor colorWithRed:0.1008 green:0.3886 blue:1.0 alpha:1.0];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        [_pageControl addTarget:self action:@selector(imageChange) forControlEvents:UIControlEventValueChanged];
    }
    [self startTimer];
    
    return self;
}


#pragma mark - 定时器开启
- (void)startTimer
{
    self.timer = [NSTimer timerWithTimeInterval:_time target:self selector:@selector(imageChange) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

#pragma mark - 拖拽开始停止时钟
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// 停止时钟
    [_timer invalidate];
}

#pragma mark - 拖拽结束开始时钟
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    // 开始
    [self startTimer];
}

#pragma mark - 定时器方法 通过偏移量实现切换图片
- (void)imageChange
{
    [self.scrollerView setContentOffset:CGPointMake(self.scrollerView.contentOffset.x + WDTH, 0) animated:YES];
    CGPoint temp = _scrollerView.contentOffset;
    if (temp.x == 0) {
        self.scrollerView.contentOffset = CGPointMake(_count *WDTH, 0);
    }
    if (temp.x == _countOne * WDTH) {
        self.scrollerView.contentOffset = CGPointMake(1 *WDTH, 0);
    }
}

#pragma mark - scrollerView 偏移量改变 实现图片切换
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint temp  = scrollView.contentOffset;
    if (temp.x == _countOne *WDTH) {
        _scrollerView.contentOffset = CGPointMake(WDTH, 0);
    } else if (temp.x == 0) {
        self.scrollerView.contentOffset = CGPointMake(_count *WDTH, 0);
    }

}

#pragma mark - 实现 PageControl 随着偏移量 改变
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger tempPage = _scrollerView.contentOffset.x / WDTH - 1;
    if (tempPage == _count) {
        tempPage = 0;
        scrollView.contentOffset = CGPointMake(WDTH, 0);
    }
    _pageControl.currentPage = tempPage;

}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
